/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.warehouse.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.warehouse.entity.Material;
import com.thinkgem.jeesite.modules.warehouse.service.MaterialService;

/**
 * MaterialController
 * 
 * @author qrg
 * @version 2017-03-27
 */
@Controller
@RequestMapping(value = "${adminPath}/warehouse/material")
public class MaterialController extends BaseController {

	@Autowired
	private MaterialService materialService;

	@ModelAttribute
	public Material get(@RequestParam(required = false) String id) {
		Material entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = materialService.get(id);
		}
		if (entity == null) {
			entity = new Material();
		}
		return entity;
	}

	@RequiresPermissions("warehouse:material:view")
	@RequestMapping(value = { "list", "" })
	public String list(Material material, HttpServletRequest request, HttpServletResponse response, Model model) {
		//Office office = UserUtils.getUser().getOffice();
		//material.setOffice(office);
		Page<Material> page = materialService.findPage(new Page<Material>(request, response), material);
		model.addAttribute("page", page);
		return "modules/warehouse/materialList";
	}

	@RequiresPermissions("warehouse:material:view")
	@RequestMapping(value = "form")
	public String form(Material material, Model model) {
		model.addAttribute("material", material);
		return "modules/warehouse/materialForm";
	}

	@RequiresPermissions("warehouse:material:edit")
	@RequestMapping(value = "save")
	public String save(Material material, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, material)) {
			return form(material, model);
		}
		materialService.save(material);
		addMessage(redirectAttributes, "保存Material成功");
		return "redirect:" + Global.getAdminPath() + "/warehouse/material/?repage";
	}

	@RequiresPermissions("warehouse:material:edit")
	@RequestMapping(value = "delete")
	public String delete(Material material, RedirectAttributes redirectAttributes) {
		materialService.delete(material);
		addMessage(redirectAttributes, "删除Material成功");
		return "redirect:" + Global.getAdminPath() + "/warehouse/material/?repage";
	}

}